#include<iostream>
//vector的增删查改的模拟实现
//模拟实现vector类，并完成测试
using namespace std;
namespace bit
{

	template<class T>

	class vector

	{

	public:

		// Vector的迭代器是一个原生指针

		typedef T* iterator;

			typedef const T* const_iterator;

	    iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator cbegin()const
		{
			return  _start;
		}

		const_iterator cend() const
		{
			return _finish;
		}


		// construct and destroy

		vector()
		{
			_start = new T[4];
			_finish = _start;
			_endOfStorage = _start + 4;
		}

		vector(int n, const T& value = T())
		{
			_start = new T[4];
			_finish = _start;
			_endOfStorage = _start + 4;
			while (n)
			{
				push_back(value);
				n--;
			}
		}

		template<class InputIterator>

		vector(InputIterator first, InputIterator last)
		{
			_start = new T[4];
			_finish = _start;
			_endOfStorage = _start + 4;
			InputIterator temp = first;
			while (temp != last)
			{
				push_back(*temp);
				temp++;
			}
		}

		vector(const vector<T>& v)
		{
			int size = v._endOfStorage - v._start;
			_start = new T[size];
			_finish = _start;
			_endOfStorage = _start + size;
		   const_iterator temp = v.cbegin();
			while (temp != v.cend())
			{
				push_back(*temp);
				temp++;
			}

		}

		vector<T>& operator= (vector<T> v)
		{
				delete[] _start;

				_start = new T[v._endOfStorage - v._start];
				_finish = _start;
				_endOfStorage = v._endOfStorage;
				for (auto& val : v)
				{
					push_back(val);
				}
			
			return *this;
		}

		~vector()
		{
			delete[] _start;
			_start = nullptr;
			_finish = nullptr;
			_endOfStorage = nullptr;
		}

		// capacity

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endOfStorage - _start;
		}

		void reserve(size_t n)
		{
			iterator new_start = new T[n];
			int size = _finish - _start;
			iterator temp = new_start;
			for (int i = 0; i < size; i++)
			{
				*temp = *(_start + i);
				temp++;
			}
			_start = new_start;
			_finish = _start + size;
			_endOfStorage = _start + n;
		}

		void resize(size_t n, const T& value = T())
		{
			while (n >= _endOfStorage - _start)
			{
				size_t cap = _endOfStorage - _start;
				reserve(cap * 2);
			}
			int size = _finish - _start;
			if (n > size)
			{
				for (int i = size; i < n; i++)
				{
					push_back(T());
				}
			}
			else
			{
				_finish = _start + n;
			}
		}



		///////////////access///////////////////////////////

		T& operator[](size_t pos)
		{
			return *(_start + pos);
		}

		const T& operator[](size_t pos)const
		{
			return  (*(_start + pos));
		}



		///////////////modify/////////////////////////////

		void push_back(const T& x)
		{
			insert(_finish, x);
		}

		void pop_back()
		{
			erase(_finish - 1);
		}

		void swap(vector<T>& v)
		{
			::swap(_start, v._start);
			::swap(_finish, v._finish);
			::swap(_endOfStorage, v._endOfStorage);
		}

		iterator insert(iterator pos, const T& x)
		{
			int n = pos - _start;
			if (_finish == _endOfStorage)
			{
				int cap = _endOfStorage - _start;
				reserve(cap * 2);
			}
			if (_start != _finish)
			{
				pos = _start + n;
				iterator _temp = _finish;
				while (_temp+1 != pos)
				{
					*_temp  = *(_temp-1);
					_temp--;
				}
			}
			*pos = x;
			_finish++;
			return pos;
		}

		iterator erase(iterator pos)
		{
			iterator _temp = pos + 1;
			while (_temp != _finish)
			{
				*(_temp - 1) = *_temp;
				_temp++;
			}
			_finish--;
			return pos;
		}
		void print()
		{
			iterator temp = _start;
			while (temp!=_finish)
			{
				cout << *temp << ' ';
				temp++;
			}
			cout << endl;
		}
	private:

		iterator _start; // 指向数据块的开始

		iterator _finish; // 指向有效数据的尾

		iterator _endOfStorage; // 指向存储容量的尾

	};

}

int main()
{
	int i = int();
	int nums[10] = { 1,2,3,4,5,6,7,8,9,10 };
	bit::vector<int>arr(nums, nums + 10);
	cout << arr.size() << endl;
	arr.resize(20);
	cout << arr.size() << endl;
	arr.push_back(11);
	for (auto& val : arr)
	{
		cout << val << ' ';
	}
	cout << endl;
	arr.pop_back();
	for (auto& val : arr)
	{
		cout << val << ' ';
	}
	cout << endl;
	cout << arr.capacity() << endl;
	bit::vector<int>arr2(10, 10);
	arr2.print();
	arr = arr2;
	arr.print();
	arr.insert(arr.begin() + 5, 6);
	arr.print();
	arr.erase(arr.begin() + 5);
	arr.print();
	for (auto& val : arr)
	{
		val++;
	}
	arr.print();
	bit::vector<int>arr3(nums, nums + 5);
	arr3.print();
	arr3.swap(arr);
	arr3.print();
	cout << arr[4] << endl;
	return 0;
}